﻿using System;
using System.Windows.Documents;

namespace Thunder.IrcClient
{
    /// <summary>
    /// Interaction logic for ChannelControl.xaml
    /// </summary>
    public partial class ChannelControl : IEquatable<string>
    {
        private readonly SortableObservableCollection<MemberInfo> channelMembers = new SortableObservableCollection<MemberInfo>();
        private readonly Meebey.SmartIrc4net.IrcClient ircClient;

        /// <summary>
        /// Gets the name of the channel
        /// </summary>
        public string ChannelName { get; private set; }

        /// <summary>
        /// Gets the list of channel members
        /// </summary>
        public SortableObservableCollection<MemberInfo> ChannelMembers
        {
            get { return this.channelMembers; }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        protected ChannelControl()
        {
            this.InitializeComponent();

            VerticalScrollHelper.ResetVerticalScroll(this.ChannelText, new TextRange(this.ChannelText.Document.ContentStart, this.ChannelText.Document.ContentEnd).Text);
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="ircClient">The <see cref="Meebey.SmartIrc4net.IrcClient"/> used for the connection</param>
        /// <param name="channelName">Name of the channel to show</param>
        public ChannelControl(Meebey.SmartIrc4net.IrcClient ircClient, string channelName) : this()
        {
            this.ChannelName = channelName;
            this.Title = this.ChannelName;

            this.ircClient = ircClient;
        }

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">
        /// An object to compare with this object.
        /// </param>
        public bool Equals(string other)
        {
            return this.Name == other;
        }

        private void Document_Closed(object sender, EventArgs e)
        {
            if (this.ircClient.JoinedChannels.Contains(this.ChannelName) == true)
            {
                this.ircClient.RfcPart(this.ChannelName);
            }
        }
    }
}